//
//  XKNewFeatureViewCell.swift
//  XKWeibo
//
//  Created by 小k on 15/11/10.
//  Copyright © 2015年 小k. All rights reserved.
//

import UIKit
import SnapKit

class XKNewFeatureViewCell: UICollectionViewCell {
    /// 定义图像索引
    var imageIndex:Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
        }
    }
    
    /// 点击开始按钮
    @objc private func clickBtn () {
        print("开始点击")
        
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewController, object: nil)
        
    }
    
    /// 显示按钮动画
        func showBtnAnimation () {
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        startBtn.userInteractionEnabled = false
        
        // 添加动画
        UIView.animateWithDuration(2,
            delay: 0,
            usingSpringWithDamping: 0.6,
            initialSpringVelocity: 7,
            options: [],
            animations: { () -> Void in
                self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startBtn.userInteractionEnabled = true
        }
    }
    
    /// 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)        // 确保控件已经添加完毕
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载控件
    /// 新特性图像
    private lazy var iconView:UIImageView = UIImageView (image: UIImage(named: "new_feature_\(self.tag)"))
    /// 开始按钮
    private lazy var startBtn:UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), imageName: "new_feature_finish_button")
}

// MARK: - 设置界面
extension XKNewFeatureViewCell {
    private func setupUI () {
        // 1. 添加控件
        addSubview(iconView)
        addSubview(startBtn)
        
        // 2. 布局控件
        iconView.frame = bounds
        
        // 不能单纯在此设置隐藏
        startBtn.hidden = true
        
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(snp_centerX)
            make.bottom.equalTo(snp_bottom).multipliedBy(0.7)
        }
        // 设置圆角
        startBtn.layer.cornerRadius = 5
        startBtn.layer.masksToBounds = true
        
        /// 添加监听方法
        startBtn.addTarget(self, action: "clickBtn", forControlEvents: .TouchUpInside)
    }
}








